****************
* daily analysis
****************
clear
set more off

use "/Users/zerenli1992/Dropbox/political_risk/data/daily_analysis_2109.dta" ,clear

* expelled_date sentenced_date
foreach x in Trddt investigated_date {
	format `x' %td
} 


sort  event_id Trddt
by  event_id : gen Trddtnum=_n
by  event_id : gen target=Trddtnum if Trddt==investigated_date
egen td=min(target), by(event_id)
drop target
gen dif=Trddtnum-td


by  event_id: gen event_window=1 if dif>= 0 & dif< 20
egen count_event_obs=count(event_window), by(event_id)
by  event_id: gen estimation_window=1 if dif< 0 & dif>= -200
egen count_est_obs=count(estimation_window), by(event_id)
replace event_window=0 if event_window==.
replace estimation_window=0 if estimation_window==.

tab count_event_obs
tab count_est_obs

drop if count_event_obs < 1

set more off /* this command just keeps stata from pausing after each screen of output */

gen predicted_return=.
egen id= group(event_id) 

sum id
global max1 = r(max)

 /* for multiple event dates, use: egen id = group(group_id) */
forvalues i= 1(1)  $max1 { 
	/*note: replace N with the highest value of id */ 
	qui l id event_id if id==`i' & dif==0
	qui reg Dretwd Cdretwdos if id==`i' & estimation_window==1 
	qui predict p if id==`i'
	qui replace predicted_return = p if id==`i' & event_window==1 
	qui drop p
}  


* abnornal return

sort id Trddt
gen abnormal_return=Dretwd-predicted_return if event_window==1
replace abnormal_return= 0 if  abnormal_return == .
drop if dif < 0 | dif> 20


by id: gen cumulative_abnormal_return = sum(abnormal_return)



*table dif ,   statistic(mean cumulative_abnormal_return) 

*table dif if  type  ==  "Purged", statistic(mean cumulative_abnormal_return) 
*table dif if  type  ==  "Survivors", statistic(mean cumulative_abnormal_return) 


saveold "/Users/zerenli1992/Dropbox/political_risk/data/daily_analysis_new.dta",replace 


/** test signifance

sort id Trddt
by id: egen ar_sd = sd(abnormal_return) 
gen test =(1/sqrt(20)) *( cumulative_abnormal_return /ar_sd) 

list Stkcd cumulative_abnormal_return test if dif==0


reg cumulative_abnormal_return if dif==0, robust

**/


